package edu.cdbi.database;

import junit.framework.TestCase;
import edu.cdbi.model.instance.Exam;

public class ExamManagementTest extends TestCase {
	
	private ExamManagement management;

	protected void setUp() throws Exception {
		super.setUp();
		management = new ExamManagement();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testInsert() {
		int id = management.insert(1, 1, 50);
		int counter = 0;
		for(Exam exam :management.getList()){
			if(exam.getId() == id){
				counter++;
				assertTrue(exam.getCourseId() == 1);
				assertTrue(exam.getStudentId() == 1);
				assertTrue(exam.getGrade() == 50);
			}
		}
		assertTrue(counter == 1);
		management.delete(id);
	}
	
	public void testGetList() {
		/*讀取2筆測試資料，測試搜尋功能是否正確
		 * 分別為
		 * 1. 2, 2, 100
		 * 2. 3, 3, 0
		 */
		int counter = 0;
		for(Exam exam : management.getList()){
			if(exam.getStudentId() == 2 && exam.getCourseId() == 2 &&
					exam.getGrade() == 100){
				counter ++;
			}else if(exam.getStudentId() == 3 && exam.getCourseId() == 3 &&
					exam.getGrade() == 0){
				counter ++;
			}
		}
		
		assertTrue(counter == 2);
	}

	public void testDelete() {
		/*刪除1筆測試資料，測試刪除功能是否正確
		 * 分別為
		 * 1. 40, 4, 4, 80
		 */
		int counter = 0;
		for(Exam exam : management.getList()){
			if(exam.getId() == 40){
				counter ++;
			}
		}
		
		assertTrue(counter == 1);
		
		management.delete(40);
		for(Exam exam : management.getList()){
			if(exam.getId() == 40){
				counter --;
			}
		}
		
		assertTrue(counter == 1);
	}

}
